package control;

import javax.microedition.lcdui.Graphics;

public class HorizontalSlider extends Control {
	protected int min;
	protected int max;
	protected int value;
	int x, y;
	int w, h;

	public HorizontalSlider(int min, int max, int x, int y, int w, int h) {
		this.min = min;
		this.max = max;
		this.value = min;
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	protected boolean isHover(int x, int y) {
		return (x > this.x && y > this.y && x <= this.x + this.w && y <= this.y
				+ this.h);
	}

	public boolean pointerPressed(int x, int y) {
		if (isHover(x, y)) {
			int l = x - this.x;
			value = min + (max - min) * l / this.w;

			if (command != null) {
				command.Do();
			}
			return true;
		}
		return false;
	}

	public boolean pointerReleased(int x, int y) {
		if (isHover(x, y)) {
			return true;
		}
		return false;
	}
	protected void controlPaint(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(0xffffffff);
		g.fillRect(x, y+5, w, h-10);
		g.setColor(0xff000000);
		g.fillRect(x+2, y+h/2-2, w-4, 4);
		/*g.setColor(0xffffffff);
		g.fillRect(x+4, y+4, w-8, h-8);*/
		
		g.setColor(0xff000000);
		int l = this.w * (value-min) / (max-min);
		g.fillRect(x + l - 3, y - 2, 6, h + 4);
	}
}
